#include <iostream>
#include <unistd.h>
#include <pthread.h>

/*
 * 给一个全局变量g_val,让一个线程进行++，其他线程会受到影响：
 */

using namespace std;
__thread int g_val = 100;

void* start_routine(void*args)
{
    string name = static_cast<const char*>(args);
    while(true)
    {
        cout<<name<<" running ... "<<"g_val: "<<g_val<<"&g_val: "<<&g_val<<endl;
        sleep(1);
        ++g_val;
    }
}

int main()
{
    pthread_t tid;
    pthread_create(&tid,nullptr,start_routine,(void*)"thread 1");
    while(true)
    {
        printf("main thread g_val: %d &g_val: 0x%x\n",g_val,&g_val);
        sleep(1);
    }
    pthread_join(tid,nullptr);
    return 0;
}

#if 0

main thread g_val: 100 &g_val: 0x617da73c
thread 1 running ... g_val: 100&g_val: 0x7f3b617d96fc
thread 1 running ... g_val: 101&g_val: 0x7f3b617d96fc
main thread g_val: 100 &g_val: 0x617da73c
main thread g_val: 100 &g_val: 0x617da73c
thread 1 running ... g_val: 102&g_val: 0x7f3b617d96fc
main thread g_val: 100 &g_val: 0x617da73c
thread 1 running ... g_val: 103&g_val: 0x7f3b617d96fc
main thread g_val: 100 &g_val: 0x617da73c
thread 1 running ... g_val: 104&g_val: 0x7f3b617d96fc
thread 1 running ... g_val: 105&g_val: 0x7f3b617d96fc


#endif
